import 'dart:io';

void main() {
  // 注册 SIGINT 处理函数
  ProcessSignal.sigint.watch().listen((ProcessSignal signal) async {
    print('Received SIGINT, shutting down gracefully...');
    var answer = await promptUser('Do you want to exit? (Y/N)');
    if (answer?.toLowerCase() == 'y') {
      exit(0); // 或者调用 isolate.kill(priority: Isolate.immediate) 来杀死当前运行的isolate
    }
  });

  // 应用程序开始运行
  runApp();
}

Future<String?> promptUser(String message) async {
  stdout.write('$message ');
  return stdin.readLineSync()?.trimRight();
}

void runApp() {
  // 运行应用程序
  print('Starting app... (press Ctrl+C to exit)');

  // while(true){
  //   print("run...");
  //   sleep(Duration(seconds: 3));
  // }
  // ...
}

Future<void> cleanup() async {
  // 清理任何需要释放的资源，例如数据库连接、文件句柄等
  print('Cleaning up resources...');
  // ...
}
